package LeetCode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}20:28
 */
public class M10 {//BFS遍历树

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x;
        }
    }

    public static void main(String[] args) {

    }

    public int[] levelOrder(TreeNode root) {
        if (root == null) return new int[0];
        Queue queue = new LinkedList();
        List<Integer> list = new ArrayList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            TreeNode treeNode = (TreeNode) queue.poll();
            if (treeNode.left!=null)
                ((LinkedList) queue).add(treeNode.left);
            if (treeNode.right!=null)
                ((LinkedList) queue).add(treeNode.right);
            list.add(treeNode.val);

        }
        int[] result = new int[list.size()];
        for (int i = 0 ; i < result.length;i++){
            result[i] = list.get(i);
        }
        return result;
    }
}
